package com.javalong.affairs.module.main.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.CircularArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.AppDataUtils;
import com.javalong.affairs.base.utils.AppUtils;
import com.javalong.affairs.module.main.activity.GuideActivity;
import com.javalong.affairs.module.main.ui.JLCheckBox;
import com.javalong.affairs.module.userModule.activity.UpdatePwdActivity;
import com.javalong.affairs.module.userModule.activity.UpdateUserInfoActivity;

/**
 * Created by javalong on 2016/5/15.
 */
public class SettingFragment extends BaseFragment implements JLCheckBox.OnCheckListener {

    private TextView tv_username;
    private TextView tv_workid;
    private TextView tv_dept;
    private TextView tv_phone;
    private JLCheckBox cb_auto;
    private JLCheckBox cb_remember;
    private Button bt_update;
    private Button bt_updatepwd;
    private Button bt_logout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        initUI(view);
        initWithData();
        return view;
    }

    private void initUI(View view) {
        tv_username = (TextView) view.findViewById(R.id.tv_username);
        tv_workid = (TextView) view.findViewById(R.id.tv_workId);
        tv_dept = (TextView) view.findViewById(R.id.tv_dept);
        tv_phone = (TextView) view.findViewById(R.id.tv_phone);
        bt_update = (Button) view.findViewById(R.id.bt_update);
        bt_updatepwd = (Button) view.findViewById(R.id.bt_updatepwd);
        bt_logout = (Button) view.findViewById(R.id.bt_logout);
        cb_auto = (JLCheckBox) view.findViewById(R.id.cb_auto);
        cb_remember = (JLCheckBox) view.findViewById(R.id.cb_remember);
    }

    private void initWithData() {
        boolean autoLogin = AppDataUtils.getBoolean(getActivity(), getString(R.string.share_autologin));
        boolean remember = AppDataUtils.getBoolean(getActivity(), getString(R.string.share_remember));
        cb_auto.initWithData(autoLogin);
        cb_remember.initWithData(remember);
        cb_auto.setOnCheckListener(this);
        cb_remember.setOnCheckListener(this);
        User user = (User) AllData.GetInstance().get(R.string.data_user_current_user);
        tv_username.setText(user.userName);
        tv_dept.setText(user.sectionName);
        tv_workid.setText(user.workId);
        tv_phone.setText(user.phone);
        bt_updatepwd.setOnClickListener(this);
        bt_update.setOnClickListener(this);
        bt_logout.setOnClickListener(this);
    }

    @Override
    public void onChecked(View v, boolean checked) {
        switch (v.getId()) {
            case R.id.cb_auto:
                AppDataUtils.saveBoolean(getContext(), getString(R.string.share_autologin), checked);
                break;
            case R.id.cb_remember:
                AppDataUtils.saveBoolean(getContext(), getString(R.string.share_remember), checked);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_logout:
                AppUtils.getInstance().exit();
                break;
            case R.id.bt_update:
                IntentManager.doActivity(getActivity(),
                        UpdateUserInfoActivity.class);
                break;
            case R.id.bt_updatepwd:
                IntentManager.doActivity(getActivity(),
                        UpdatePwdActivity.class);
                break;
        }
    }
}
